<?php
/*/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020-02-23
 * Time: 10:07
 */

namespace app\cms\controller;


use app\admin\controller\Base;
use app\cms\model\News as NewsModel;
use app\cms\model\Newsclass as NewsclassModel;
use think\Config;

class News extends Base
{
    public function index(){
        $list = NewsModel::where('title|keyboard|newstext','like','%'.trim($this->request->param('keywords')).'%')
            ->order('newstime','desc')
            ->paginate(Config::get('paginate')['list_rows']);
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function add(){
        $this->assign('uploadset',Config::get('uploadfile'));
        $plist = NewsclassModel::where('pid',0)->where('status',1)->select();
        $this->assign('plist',$plist);
        return $this->fetch();
    }

    public function adddo(){
        $data = array(
            'befrom' => $this->request->param('befrom'),
            'classid' => $this->request->param('classid'),
            'uname' => $this->request->session('kj.uname'),
            'userid' => $this->request->session('kj.uid'),
            'ftitle' => $this->request->param('ftitle'),
            'isgood' => $this->request->param('isgood'),
            'istop' => $this->request->param('istop'),
            'newstext' => $this->request->param('newstext'),
            'title' => $this->request->param('title'),
            'titlepic' => $this->request->param('titlepic'),
            'writer' => $this->request->param('writer'),
            'smalltext' => $this->request->param('smalltext'),
        );

        if(count($data)>=5){
            $check = NewsModel::where('title',$data['title'])->find();
            if($check==false){
                if($insert = NewsModel::create($data)){
                    $res = ['statusCode'=>200,'message'=>'操作完成'];
                }else{
                    $res = ['statusCode'=>300,'message'=>'数据操作失败'];
                }
            }else{
                $res = ['statusCode'=>300,'message'=>'对不起，您录入的文章标题已存在'];
            }
        }else{
            $res = ['statusCode'=>300,'message'=>'数据录入错误'];
        }
        echo json_encode($res);
    }

    public function edit(){
        $this->assign('uploadset',Config::get('uploadfile'));
        $plist = NewsclassModel::where('pid',0)->where('status',1)->select();
        $this->assign('plist',$plist);
        $this->assign('info',NewsModel::get($this->request->param('id')));
        return $this->fetch();
    }

    public function editdo(){
        $data = array(
            'id' => $this->request->param('id'),
            'befrom' => $this->request->param('befrom'),
            'classid' => $this->request->param('classid'),
            'uname' => $this->request->session('kj.uname'),
            'userid' => $this->request->session('kj.uid'),
            'ftitle' => $this->request->param('ftitle'),
            'isgood' => $this->request->param('isgood'),
            'istop' => $this->request->param('istop'),
            'newstext' => $this->request->param('newstext'),
            'title' => $this->request->param('title'),
            'writer' => $this->request->param('writer'),
            'smalltext' => $this->request->param('smalltext'),
        );

        if(count($data)>=6){
            $titlepic = $this->request->param('titlepic');
            if(!empty($titlepic)){
                $data['titlepic'] = $titlepic;
            }
            $check = NewsModel::where('title',$data['title'])->where('id','<>',$data['id'])->find();
            if($check==false){
                if($update = NewsModel::update($data)){
                    $res = ['statusCode'=>200,'message'=>'操作完成'];
                }else{
                    $res = ['statusCode'=>300,'message'=>'数据操作失败'];
                }
            }else{
                $res = ['statusCode'=>300,'message'=>'对不起，您录入的文章标题已存在'];
            }
        }else{
            $res = ['statusCode'=>300,'message'=>'数据录入错误'];
        }
        echo json_encode($res);
    }

    public function delete(){
        $ids = explode(',',$this->request->param('ids'));
        $i = 0;
        if(count($ids)>0){
            foreach($ids as $key=>$value){
                $del = NewsModel::get($value);
                if($del['checked']=='未审核'){
                    $path = $del->titlepic;
                    if(!empty($path)){
                        $d = explode('upload',$path);
                        $d = 'upload'.$d[1];
                        if(file_exists($d)){
                            unlink($d);
                        }
                    }
                    NewsModel::destroy($value);
                    $i++;
                }
            }
            if($i>0){
                $res = ['statusCode'=>200,'message'=>'操作完成'];
            }else{
                $res = ['statusCode'=>300,'message'=>'请先关闭操作项后在执行此操作'];
            }
        }else{
            $res = ['statusCode'=>300,'message'=>'操作失败'];
        }
        echo json_encode($res);
    }

    public function set(){
        $status = $this->request->param('status');
        $id = $this->request->param('id');
        if($id){
            NewsModel::where('id',$id)->update(['checked'=>$status]);
            $res = ['statusCode'=>200,'message'=>'操作完成'];
        }else{
            $res = ['statusCode'=>300,'message'=>'非法操作'];
        }
        echo json_encode($res);
    }

    public function sort(){
        $sort = $this->request->param('sort');
        $id = $this->request->param('id');
        if($id){
            NewsModel::where('id',$id)->update(['newstime'=>strtotime($sort)]);
            $res = ['statusCode'=>200,'message'=>'操作完成'];
        }else{
            $res = ['statusCode'=>300,'message'=>'非法操作'];
        }
        echo json_encode($res);
    }


}